package com.example.petshopping.ui.address.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;

import java.util.ArrayList;
import java.util.List;

public class RegionAdapter extends RecyclerView.Adapter<RegionAdapter.RegionViewHolder> {

    private Context context;
    private List<String> regions;
    private String selectedRegion;
    private OnRegionSelectedListener listener;

    public RegionAdapter(Context context) {
        this.context = context;
        this.regions = new ArrayList<>();
    }

    public void setOnRegionSelectedListener(OnRegionSelectedListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RegionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_region, parent, false);
        return new RegionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RegionViewHolder holder, int position) {
        String region = regions.get(position);
        holder.tvRegionName.setText(region);

        if (region.equals(selectedRegion)) {
            holder.tvRegionName.setTextColor(context.getResources().getColor(android.R.color.holo_purple));
            holder.ivSelected.setVisibility(View.VISIBLE);
        } else {
            holder.tvRegionName.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
            holder.ivSelected.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                selectedRegion = region;
                listener.onRegionSelected(region, position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return regions.size();
    }

    public void updateData(List<String> regions) {
        this.regions.clear();
        if (regions != null) {
            this.regions.addAll(regions);
        }
        notifyDataSetChanged();
    }

    public void setSelectedRegion(String region) {
        this.selectedRegion = region;
        notifyDataSetChanged();
    }

    public String getSelectedRegion() {
        return selectedRegion;
    }

    public static class RegionViewHolder extends RecyclerView.ViewHolder {
        TextView tvRegionName;
        ImageView ivSelected;

        public RegionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRegionName = itemView.findViewById(R.id.tv_region_name);
            ivSelected = itemView.findViewById(R.id.iv_selected);
        }
    }

    public interface OnRegionSelectedListener {
        void onRegionSelected(String region, int position);
    }
} 